﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Steamrunner
{
    class CameraManager
    {
        List<Camera2D> cameras = null;
        List<Viewport> viewports = null;
        List<bool> cameraInUse = null;

        int screenWidth;
        int screenHeight;

        public CameraManager(int screenWidth, int screenHeight)
        {
            this.screenHeight = screenHeight;
            this.screenWidth = screenWidth;
        }

        public void Initialize()
        {
            cameras = new List<Camera2D>();
            viewports = new List<Viewport>();
            cameraInUse = new List<bool>();

            Viewport vp = new Viewport();
            vp.X = 0;
            vp.Y = 0;
            vp.Width = screenWidth / 2;
            vp.Height = screenHeight / 2;
            viewports.Add(vp);

            vp = new Viewport();
            vp.X = screenWidth / 2;
            vp.Y = 0;
            vp.Width = screenWidth / 2;
            vp.Height = screenHeight / 2;
            viewports.Add(vp);

            vp = new Viewport();
            vp.X = 0;
            vp.Y = screenHeight / 2;
            vp.Width = screenWidth / 2;
            vp.Height = screenHeight / 2;
            viewports.Add(vp);

            vp = new Viewport();
            vp.X = screenWidth / 2;
            vp.Y = screenHeight / 2;
            vp.Width = screenWidth / 2;
            vp.Height = screenHeight / 2;
            viewports.Add(vp);

            foreach (Viewport view in viewports)
            {
                Camera2D cam = new Camera2D();
                cam.Initialize(view);
                cameras.Add(cam);
            }

            IniFile config = new IniFile("config.ini");
            config.Parse();

            int amountOfPlayers = (int)config.GetValue("Players", "Amount");
            for (int i = 0; i < 4; i++)
            {
                if (i < amountOfPlayers)
                {
                    cameraInUse.Add(true);
                }
                else
                {
                    cameraInUse.Add(false);
                }
            }
        }

        public void Update(List<Vector2> positions)
        {
            for (int i = 0; i < cameras.Count; i++)
            {
                if (cameraInUse[i])
                {
                    cameras[i].Position = positions[i];
                    cameras[i].Update();
                }
            }
        }

        public Viewport GetViewport(int i)
        {
            return viewports[i];
        }

        public Camera2D GetCamera(int i)
        {
            return cameras[i];
        }

        public bool GetCameraInUse(int i)
        {
            return cameraInUse[i];
        }
    }
}
